import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
// 路由懒加载
const Home = () => import('@/views/home/index.vue')
const Login = () => import('@/views/login/index.vue')
const Question = () => import('@/views/question/index.vue')
const User = () => import('@/views/user/index.vue')
const Video = () => import('@/views/video/index.vue')
const Tabbar = () => import('@/components/app_tabbar.vue')
const Chat = () => import('@/views/user/chat.vue')
const Profile = () => import('@/views/user/profile.vue')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    alias: '/home',
    components: {
      // default对应路由出口的name Home对应路由组件
      default: Home,
      bottom: Tabbar
    }
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/question',
    components: {
      default: Question,
      bottom: Tabbar
    }
  },
  {
    path: '/user',
    components: {
      default: User,
      bottom: Tabbar
    },
    // 路由元
    meta: {
      isLogin: true
    }
  },
  {
    path: '/user/chat',
    component: Chat
  },
  {
    path: '/user/profile',
    component: Profile
  },
  {
    path: '/video',
    components: {
      default: Video,
      bottom: Tabbar
    }
  }
]

const router = new VueRouter({
  routes
})
// 导航守卫
// const whitePath = ['/user']
router.beforeEach((to, from, next) => {
  // 如果有token的情况
  if (store.getters['user/getToken']) {
    if (to.path.toLowerCase() === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    // 没有token的情况 判断 1.白名单 2.路由元
    // 1/白名单
    // if (whitePath.includes(to.path.toLowerCase())) {
    //   next('/login')
    // } else {
    //   next()
    // }
    // 2/路由元
    if (to.meta.isLogin) {
      next('/login')
    } else {
      next()
    }
  }
})

export default router
